package routers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 初始化/user/*相关路由
func InitUserRouter(r *gin.Engine) {
	userGroup := r.Group("/user")
	{
		// 方式一：/user/info?userid=value1&username=v2
		userGroup.GET("/info", func1)

		// 方式二：/user/info/:userid/:username
		userGroup.GET("/info/:userid/:username", func2)
	}
}

func func1(context *gin.Context) {
	// 常规问号参数，使用Query获取
	userid := context.Query("userid")
	username := context.Query("username")
	context.JSON(http.StatusOK, gin.H{
		userid:   userid,
		username: username,
	})
}

func func2(context *gin.Context) {
	// restful风格，路径参数使用Param获取
	userid := context.Param("userid")
	username := context.Param("username")
	fmt.Println(username)
	context.JSON(http.StatusOK, gin.H{
		"descript": "this is restful...",
		userid:     userid,
		username:   username,
	})
}
